var orderController;
var payTypeController,confirmController;
$(function () {
    payTypeController = new Vue({
        el:'#payTypeController',
        data:{
            showPayType:false,
            payType:1
        },
        methods:{
            selectPayType:function (type) {
                this.payType = type;
            },
            setType:function (value) {
                this.showPayType  =value;
            },
            toPay:function () {
                orderController.toPay(this.payType);
            }
        }
    });
    confirmController = new Vue({
       el:'#confirmController',
       data:{
           orderConfirm:false
       },
        methods:{
            setOrderConfirm:function (val) {
                this.orderConfirm = val;
                console.log(this.orderConfirm)
            },
            confirmOrder:function () {
                orderController.confirmOrder();
            }
        }
    });
    orderController = new Vue({
        el:'#orderController',
        data:{
            state:0,
            list:[],
            page:1,
            limit:10,
            busy:false,
            hasMore:true,
            orderId:'',
            orderConfirm:false,
            order:{}
        },
        methods:{
            showPay:function (order) {
                this.orderId = order.id;
                payTypeController.showPayType = true;
            },
            toPay:function (payType) {
                sy.post('/web/pay/order/'+this.orderId,{
                    payType:payType
                },function (data) {
                    if (data.errorCode){
                        sy.msg(data.errorMsg);
                        return;
                    }
                    if (payType == 1){
                        //支付宝
                        $("#content").val(data.pay);
                        $("#form").submit();
                    }else{
                        //微信二维码
                        var url = sy.path + '/qr?msg='+data.qr+'&size=150';
                        sy.showWxQrPay(data.price,url,function(){window.location.href = sy.path + "/web/home/order";},null);
                    }
                })
            },
            loadOrders:function () {
                var that = this;
                if (!that.hasMore){
                    return;
                }
                that.busy = true;
                sy.postApi('/api/userInfo/getMyOrder',{
                    state:that.state,
                    page:that.page,
                    limit:that.limit
                },function (data) {
                    $.each(data,function(i,o){
                        that.list.push(o);
                    });
                    if(data.length < that.limit){
                        that.hasMore = false;
                    }else{
                        that.busy = false;
                        that.page ++;
                    }
                })
            },
            setState:function (state) {
                if (this.state != state){
                    this.state = state;
                    this.reloadData();
                }
            },
            reloadData:function () {
                this.page = 1;
                this.limit = 10;
                this.busy = false;
                this.hasMore = true;
                this.list = [];
                this.loadOrders();
            },
            cancelOrder:function (order) {
                var that = this;
                sy.postApi('/api/userInfo/cancelOrder',{
                    id:order.id
                },function (data) {
                    that.reloadData();
                })
            },
            setOrderConfirm:function (data,order) {
                  confirmController.setOrderConfirm(data);
                  if (data){
                      this.order = order;
                  }else{
                      this.order = {};
                  }
            },
            confirmOrder:function (order) {
                var that = this;
                if (!that.order.id){
                    that.setOrderConfirm(false);
                    return;
                }
                sy.postApi('/api/userInfo/confirmOrder',{
                    id:that.order.id
                },function (data) {
                    that.setOrderConfirm(false);
                    that.reloadData();
                })
            },
            express:function (order) {
                sy.postApi('/api/userInfo/getExpressInfo',{
                    orderId:order.id
                },function (data) {
                    layer.open({
                        title:null,
                        type: 2,
                        content: [data, 'no'], //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
                        area: ['320px', '600px'],
                        shadeClose:true
                    });
                })
            },
            jump:function (order) {
                location.href = sy.path+"/web/home/order/"+order.id;
            }
        },
        created:function () {
            this.loadOrders();
        }
    })
});
